<?php
require_once '../../controller.php';
require_once '../../classes/Survey.php';
require_once '../../classes/Question.php'; 

if(isset($_SESSION['logged_in'])){
	$instructor_id = $_SESSION['logged_in'];
} 

if(isset($_REQUEST['sid'])){
 $sid = $_REQUEST['sid'];
 
 $today_date = date('Y-m-d'); 
 $sql = "INSERT INTO `survey` (`instructor_id`,`survey_name`,`survey_title`,`welcome_message`,`confirmation_message`,`email_subject`,`email_message`)
            SELECT `instructor_id`,`survey_name`,`survey_title`,`welcome_message`,`confirmation_message`,`email_subject`,`email_message` FROM `survey` WHERE `id` = '$sid' AND `instructor_id` = '$instructor_id'";
			$result = mysql_query($sql) or die (mysql_error());

			$sql1 = "SELECT MAX(`id`)FROM `survey` WHERE `instructor_id` = '$instructor_id'";
			$sid1 = mysql_query($sql1)or die(mysql_error());
			$result = mysql_fetch_row($sid1);
			foreach($result as $key=>$value){
				$survey_id = $value;
			}

			$get_name_query = "SELECT `survey_name` FROM `survey` WHERE `id` = '$survey_id'";
			$execute = mysql_query($get_name_query)or die(mysql_error());
			$result = mysql_fetch_row($execute);

			foreach($result as $key=>$value){
				$survey_name = $value;
			}
			$new_name = addslashes($survey_name.'_copy');
			$update_copied_survey = "UPDATE `survey` SET `survey_name` = '$new_name', `date_created`='$today_date' WHERE `id` = '$survey_id'";
			$result = mysql_query($update_copied_survey)or die("Cannot update copied survey anme and date:".mysql_error());
/**************************************************************************************************************/
           $q = new Question();
           $s = new Survey();
           $questions = $s->getQuestions($sid);
			
			if($questions){
				foreach($questions as $i=>$value){
				  $data = array(
	              'survey_id'=>$survey_id,
	              'type'=>$value->type,
	              'required'=>$value->required,
	              'question'=>addslashes($value->question),
	              'create_section'=>$value->create_section,
	              'order'=>$value->order,
	              'response1'=>addslashes($value->response1),
				  'response2'=>addslashes($value->response2),
				  'response3'=>addslashes($value->response3),
				  'response4'=>addslashes($value->response4),
				  'response5'=>addslashes($value->response5),
				  'response6'=>addslashes($value->response6),
	              
	            );
	            
	            $q->addQuestion($data);
				}
			}
			
			
			
			
			header("location: interface.php?copy=Y");
			
}			
?>